/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Proxy;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author Andreea
 */
public class MyFile implements FileInterface{
    private String name;
    private long size;
    private String path;
    private long dateModified;
    private String content;

    public MyFile(String name, long size, String path, long dateModified) {
        this.name = name;
        this.size = size;
        this.path = path;
        this.dateModified = dateModified;
        try{
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(path);
            StringWriter stringWriter = new StringWriter(); 
            Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
            transformer.transform(new DOMSource(doc), new StreamResult(stringWriter)); 
            String strFileContent = stringWriter.toString(); //This is string data of xml file
            this.setContent(strFileContent);
            //System.out.println(strFileContent);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public MyFile(String path) {
        this.path = path;
        try{
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(path);
            StringWriter stringWriter = new StringWriter(); 
            Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
            transformer.transform(new DOMSource(doc), new StreamResult(stringWriter)); 
            String strFileContent = stringWriter.toString(); //This is string data of xml file
            this.setContent(strFileContent);
            //System.out.println(strFileContent);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }


    public long getDateModified() {
        return dateModified;
    }

    public String getName() {
        return name;
    }

    public String getPath() {
        return path;
    }

    public long getSize() {
        return size;
    }

    public void setDateModified(long dateModified) {
        this.dateModified = dateModified;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String showInfo() {
       
        return this.getContent();
        //this.setContent(strFileContent);

    }

    @Override
    public String toString() {
        String newline = System.getProperty("line.separator");
        return "<html>File{" + "Name=" + name +"<br />"+ "Size=" + size +"<br />"+ "Path=" + path +"<br />"+ " dateModified=" + new Date(dateModified) + "</html>";
    }
    
}
